package com.gitee.automap.test;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/exams")
public class ExamController {
    private static final Map<String, Exam> CACHE = new ConcurrentHashMap<>();

    @GetMapping
    public Collection<Exam> getExams() {
        return CACHE.values();
    }

    /**
     * 传进来ExamCreationDTO对象由ModelMapper转换成Exam实体
     *
     * @param exam 转换后的Exam实体
     * @see org.modelmapper.ModelMapper
     */
    @PostMapping
    public void newExam(@DTO(ExamCreationDTO.class) Exam exam) {
        CACHE.put(exam.getTitle(), exam);
    }

    @PutMapping
    @ResponseStatus(HttpStatus.OK)
    public void editExam(@DTO(ExamUpdateDTO.class) Exam exam) {
        CACHE.put(exam.getTitle(), exam);
    }
}
